<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['orderlog_status']=$this->orderlog_status;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params2=array('uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
		$workerid=pdo_getall('jy_shopworker_list',$params2,array('title','id'),'id');
		if($workerid){
			$workerid=array_keys($workerid);
		}else{
			$workerid=-1;
		}
		$params['workerid']=$workerid;
	}
	if($_RQ['cname']){
		$params['cname like']='%'.trim($_RQ['cname']).'%';
		$return['params']['cname']=$_RQ['cname'];
	}
	if($_RQ['mobile']){
		$params['mobile like']='%'.trim($_RQ['mobile']).'%';
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['username']){
		$params['username like']='%'.trim($_RQ['username']).'%';
		$return['params']['username']=$_RQ['username'];
	}
	if($_RQ['status']){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['y']) && !empty($_RQ['m'])){
		$yuestart = mktime(0,0,0,$_RQ['m'],1,$_RQ['y']);
		$yueend = mktime(0,0,0,$_RQ['m']+1,1,$_RQ['y']);
		$params['pay_time between']=array($yuestart,$yueend);
	}elseif(!empty($_RQ['y'])){
		$yuestart = mktime(0,0,0,1,1,$_RQ['y']);
		$yueend = mktime(0,0,0,1,1,$_RQ['y']+1);
		$params['pay_time between']=array($yuestart,$yueend);
	}
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_shopworker_orderlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['goodsinfo']=json_decode($item['goodsinfo'],true);
		$item['lifee']=price($item['price']-$item['benprice'],'int');
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'业绩表列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'workerid','title'=>'员工','type'=>'select','options'=>'workers'),
			array('name'=>'goodsinfo.title','title'=>'商品名称','type'=>'string'),
			array('name'=>'price','title'=>'销售价','type'=>'string'),
			array('name'=>'lifee','title'=>'利润','type'=>'string'),
			array('name'=>'benprice','title'=>'当时成本价','type'=>'string'),
			array('name'=>'payimg','title'=>'支付凭证','type'=>'image'),
			array('name'=>'ordersn','title'=>'支付单号','type'=>'string'),
			array('name'=>'cname','title'=>'企业名称','type'=>'string'),
			array('name'=>'username','title'=>'联系人姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'联系人电话','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'orderlog_status'),
			array('name'=>'upworkerid','title'=>'审批人','type'=>'select','options'=>'workers'),
			array('name'=>'pay_time','title'=>'付款时间','type'=>'time'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','action'=>'jy_shopworker/orderlog/sheck','title'=>'审核','show'=>array('status'=>0)),
			array('type'=>'href','action'=>'jy_shopworker/orderlog/edit','title'=>'编辑'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'10','title'=>'员工姓名','name'=>'title','help'=>''),
			array('name'=>'cname','width'=>'10','title'=>'企业名称','type'=>'string'),
			array('name'=>'username','width'=>'10','title'=>'联系人姓名','type'=>'string'),
			array('name'=>'mobile','width'=>'10','title'=>'联系人电话','type'=>'string'),
			array('name'=>'y','width'=>10,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>10,'title'=>'月份','type'=>'radio','options'=>'months'),
			array('name'=>'status','width'=>'10','title'=>'状态','type'=>'radio','options'=>'orderlog_status'),
		),
	);
	$return['workers']=pdo_getall('jy_shopworker_list',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_shopworker_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'01'=>'1月',
		'02'=>'2月',
		'03'=>'3月',
		'04'=>'4月',
		'05'=>'5月',
		'06'=>'6月',
		'07'=>'7月',
		'08'=>'8月',
		'09'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='sheck'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
			'status'=>trim($postdata['status']),
			'failremark'=>trim($postdata['failremark']),
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_shopworker_orderlog',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			exi('审批成功！','','/orderlog/list/');
		}else{
			exi('没有找到审批');
	}
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_shopworker_orderlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['goodsinfo']=json_decode($item['goodsinfo'],true);
	$item['lifee']=price($item['price']-$item['benprice'],'int');
	$return['item']=$item;
	$return['htmls']=array(
			'type'=>'edit',
			'edit'=>array(
				array('name'=>'goodsinfo.title','title'=>'商品名称','type'=>'copy'),
				array('name'=>'price','title'=>'销售价','type'=>'copy'),
				array('name'=>'lifee','title'=>'利润','type'=>'copy'),
				array('name'=>'benprice','title'=>'当时成本价','type'=>'copy'),
				array('name'=>'payimg','title'=>'支付凭证','type'=>'img_show'),
				array('name'=>'ordersn','title'=>'支付单号','type'=>'copy'),
				array('name'=>'cname','title'=>'企业名称','type'=>'copy'),
				array('name'=>'username','title'=>'联系人姓名','type'=>'copy'),
				array('name'=>'mobile','title'=>'联系人电话','type'=>'copy'),
				array('name'=>'status','title'=>'审批状态','type'=>'radio','options'=>'status'),
				array('name'=>'failremark','title'=>'审核不通过原因','type'=>'text','show'=>array('status'=>2)),
			),
	);
	$return['status']=array('待审核','审核通过','审核不通过');
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$pay_time = strtotime($postdata['pay_time']);
		$pay_time = $pay_time?:time();
		$data=array(
			'goodsinfo'=>ijson_encode($postdata['goodsinfo']),
			'price'=>trim($postdata['price']),
			'benprice'=>trim($postdata['benprice']),
			'payimg'=>trim($postdata['payimg']),
			'ordersn'=>trim($postdata['ordersn']),
			'cname'=>trim($postdata['cname']),
			'username'=>trim($postdata['username']),
			'mobile'=>trim($postdata['mobile']),
			'pay_time'=>$pay_time,
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_shopworker_orderlog',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			exi('修改成功！','','/orderlog/list/');
		}else{
			exi('没有找到订单');
	}
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_shopworker_orderlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['pay_time']=date('Y-m-d H:i:s',$item['pay_time']);
	$item['goodsinfo']=json_decode($item['goodsinfo'],true);
	$item['lifee']=price($item['price']-$item['benprice'],'int');
	$return['item']=$item;
	$return['htmls']=array(
			'type'=>'edit',
			'edit'=>array(
				array('name'=>'goodsinfo.title','title'=>'商品名称','type'=>'string'),
				array('name'=>'price','title'=>'销售价','type'=>'string'),
				array('name'=>'benprice','title'=>'当时成本价','type'=>'string'),
				array('name'=>'payimg','title'=>'支付凭证','type'=>'image'),
				array('name'=>'ordersn','title'=>'支付单号','type'=>'string'),
				array('name'=>'pay_time','title'=>'支付时间','type'=>'time'),
				array('name'=>'cname','title'=>'企业名称','type'=>'string'),
				array('name'=>'username','title'=>'联系人姓名','type'=>'string'),
				array('name'=>'mobile','title'=>'联系人电话','type'=>'string'),
			),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	exi('暂时不支持删除表！','error');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_shopworker_orderlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_shopworker_orderlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params2=array('uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
		$workerid=pdo_getall('jy_shopworker_list',$params2,array('title','id'),'id');
		if($workerid){
			$workerid=array_keys($workerid);
		}else{
			$workerid=-1;
		}
		$params['workerid']=$workerid;
	}
	if($_RQ['cname']){
		$params['cname like']='%'.trim($_RQ['cname']).'%';
		$return['params']['cname']=$_RQ['cname'];
	}
	if($_RQ['mobile']){
		$params['mobile like']='%'.trim($_RQ['mobile']).'%';
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['username']){
		$params['username like']='%'.trim($_RQ['username']).'%';
		$return['params']['username']=$_RQ['username'];
	}
	if($_RQ['status']){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['y']) && !empty($_RQ['m'])){
		$yuestart = mktime(0,0,0,$_RQ['m'],1,$_RQ['y']);
		$yueend = mktime(0,0,0,$_RQ['m']+1,1,$_RQ['y']);
		$params['pay_time between']=array($yuestart,$yueend);
	}elseif(!empty($_RQ['y'])){
		$yuestart = mktime(0,0,0,1,1,$_RQ['y']);
		$yueend = mktime(0,0,0,1,1,$_RQ['y']+1);
		$params['pay_time between']=array($yuestart,$yueend);
	}
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$order=pdo_getall('jy_shopworker_orderlog',$params);
	foreach($order as &$item){
		$item['goodsinfo']=json_decode($item['goodsinfo'],true);
		$item['goodsinfotitle']=$item['goodsinfo']['title'];
		$item['lifee']=price($item['price']-$item['benprice'],'int');
		$item['statustitle']=$this->orderlog_status[$item['status']];
		$item['workertitle']=pdo_getcolumn('jy_shopworker_list',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'title');
		$item['upworkertitle']=pdo_getcolumn('jy_shopworker_list',array('id'=>$item['upworkerid'],'uniacid'=>$_SESSION['uniacid']),'title');
		$item['pay_time']=$item['pay_time']?date('Y-m-d H:i:s',$item['pay_time']):'';
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	}
	$header=array(
		'workertitle'=>'员工',
		'goodsinfotitle'=>'商品名称',
		'price'=>'销售价',
		'lifee'=>'利润',
		'benprice'=>'当时成本价',
		'ordersn'=>'支付单号',
		'cname'=>'企业名称',
		'username'=>'联系人姓名',
		'mobile'=>'联系人电话',
		'status'=>'状态',
		'upworkerid'=>'审批人',
		'pay_time'=>'付款时间',
		'createtime'=>'创建时间',
	);
	cfc('tools')->export2excel($header,$order,'业绩表');
}